package com.hourse.app.service.impl;

import com.hourse.app.entity.CurrencyEntity;
import com.hourse.app.service.ConfigCurrencyService;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.service.impl.PublicServiceImpl;
import com.hourse.platform.utils.AppTools;
import com.hourse.platform.utils.SqlParams;
import com.hourse.platform.utils.StringTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @作者：lichenguang
 * @描述：【平台对应币种】
 * @创建：2022/2/17 20:09
 */
@Service
public class ConfigCurrencyServiceImpl extends PublicServiceImpl implements ConfigCurrencyService {

  /**
   * @描述：【配置币种-查询】
   * @作者：lichenguang
   * @时间: 2022/2/17 20:12
   */
  @Override
  @Transactional(readOnly = true)
  public List<Object> list() {
    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" select ");
    sqlParams.append(" new com.hourse.app.vo.rms.CurrencyVo(a.id,a.name,a.code,a.state)");
    sqlParams.append(" from CurrencyEntity a order by a.createTime desc");

    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @描述：【配置币种-新增/修改】
   * @作者：lichenguang
   * @时间: 2022/2/17 20:20
   */
  @Override
  @Transactional(rollbackFor = AppException.class)
  public void save(CurrencyEntity currencyEntity) throws Exception {
    if (StringTools.isNotNull(currencyEntity.getId())) {
      this.insert(currencyEntity);
    } else {
      this.update(currencyEntity);
    }
  }

  private void insert(CurrencyEntity currencyEntity) {
    currencyEntity.setCreateTime(new Date());
    currencyEntity.setCreateUser(AppTools.GetCurrentUserId());
    this.insert(currencyEntity);
  }

  private void update(CurrencyEntity currencyEntity) throws Exception {
    SqlParams sqlParams = new SqlParams();
    sqlParams.append("update CurrencyEntity a set a.updateTime=:updateTime");
    sqlParams.append(",a.updateUser=:updateUser,a.name=:name,a.code=:code,state=:state");
    sqlParams.append(" where a.id = :id");

    sqlParams.add("updateTime", new Date());
    sqlParams.add("updateUser", AppTools.GetCurrentUserId());
    sqlParams.add("name", currencyEntity.getName());
    sqlParams.add("code", currencyEntity.getCode());
    sqlParams.add("state", currencyEntity.getState());

    baseDao.executeUpdate(sqlParams.getSql(), sqlParams.getParams());

  }
}
